﻿-- =============================================
-- Author:		Andreas Könighofer (IT-eXperience)
-- Create date: 15.01.2009
-- Description:	Reads all rentalservices that match with the given parameters
-- =============================================
CREATE PROCEDURE [dbo].[ReadRentalService]
	-- Add the parameters for the stored procedure here
	@RentalServiceID INT = NULL
	, @StartDateFrom SMALLDATETIME = NULL
	, @StartDateTo SMALLDATETIME = @StartDateFrom -- if no @StartDateTo is supplied by caller, @StartDateTo is set to @StartDateFrom
	, @EndDateFrom SMALLDATETIME = NULL
	, @EndDateTo SMALLDATETIME = @EndDateFrom -- if no @EndDateTo is supplied by caller, @EndDateTo is set to @EndDateFrom
	, @ReturnedDateFrom SMALLDATETIME = NULL
	, @ReturnedDateTo SMALLDATETIME = @ReturnedDateFrom -- if no @ReturnedDateTo is supplied by caller, @ReturnedDateTo is set to @ReturnedDateFrom
	, @ResourceID INT = NULL
	, @CustomerID INT = NULL
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT
	RentalServiceID,
	StartDate,
	EndDate,
	ReturnedDate,
	RentalService.ResourceID,
	Resource.RessourceName,
	RentalService.CustomerID,
	Customer.CustomerName
	FROM RentalService
	, Resource
	, Customer
	WHERE ((@RentalServiceID IS NULL) OR (RentalServiceID = @RentalServiceID))
	AND ((@StartDateFrom IS NULL OR @StartDateTo IS NULL) OR (StartDate BETWEEN @StartDateFrom AND @StartDateTo))
	AND ((@EndDateFrom IS NULL OR @EndDateTo IS NULL) OR (EndDate BETWEEN @EndDateFrom AND @EndDateTo))
	AND ((@ReturnedDateFrom IS NULL OR @ReturnedDateTo IS NULL) OR (ReturnedDate BETWEEN @ReturnedDateFrom AND @ReturnedDateTo))
	AND ((@ResourceID IS NULL) OR (RentalService.ResourceID = @ResourceID))
	AND ((@CustomerID IS NULL) OR (RentalService.CustomerID = @CustomerID))
	AND RentalService.CustomerID = Customer.CustomerID
	AND RentalService.ResourceID = Resource.ResourceID
	
END

---- end script ReadRentalService stored procedure ----

---- start script ReadResource stored procedure ----

